<?php
    $operacao   = addslashes($_POST['operacao']);
    $comum      = addslashes($_GET['comum']);
?>
<html lang="pt-br">
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript">
        $(document).ready(function(){
        $("#nome").focus();
        $(".cpf").hide();
        $(".cnpj").hide();
        $(".cpf").mask("999.999.999-99");
        $(".cnpj").mask("99.999.999/9999-99");
        
        $(".pessoa1").click(function (){
            $(".cpf").show();
            $(".cpf").focus();
            $(".cnpj").hide();
        });
        $(".pessoa2").click(function (){
            $(".cnpj").show();
            $(".cnpj").focus();
            $(".cpf").hide();
        });
        
        $(".fisica").click(function(){
           //$("#cadastro").val("");
           $("#cadastro").addClass("cpf");
           $("#cadastro").focus();
        });
        
        $(".juridica").click(function(){
           //$("#cadastro").val("");
           $("#cadastro").addClass("cnpj");
           $("#cadastro").focus();
        });
        
        });
        </script>
    </head>
    <body>
        <?php
        if ($comum == 'cadDestinatario'){
            ?>
        <h2 align="left">Destinatários cadastrados</h2><p>&nbsp;</p>
            <form action="/" method="GET">
                <input type="hidden" name="comum" value="cadNewDestinatario" />
                <table>
                    <tr>
                        <td></td>
                        <td><input type='submit' value="Novo"/></td>
                    </tr>
                </table>
            </form>
        <p>&nbsp;</p>
            <?php
            include_once 'modelo/modeloDestinatario.php';
            $destinatarios = getDestinatario();
            $numDestinatarios = mysql_num_rows($destinatarios);
                echo '<table class="relatorio"><tr>';
                echo '<th width="150">Destinatario</th>';
                echo '<th width="50">Pessoa</th>';
                echo '<th width="100">CPF/CNPJ</th>';
                echo '<th width="150">Endereço</th>';
                echo '<th width="100">Município</th>';
                echo '<th width="100">Editar</th>';
                echo '</tr>';
                
            if ($numDestinatarios == 0){
                echo '<tr><td colspan="7">Nenhum destinatário cadastrado</td></tr>';
            }
            for ($i=0;$i<$numDestinatarios;$i++){
                mysql_data_seek($arrayDestinatarios, $i);
                $arrayDestinatarios = mysql_fetch_array($destinatarios);
                
                if ($arrayDestinatarios['pessoa'] == 1){
                    $pessoa = 'Física';
                    $cadastro = $arrayDestinatarios['cpf'];
                }
                else if ($arrayDestinatarios['pessoa'] == 2){
                    $pessoa = 'Jurídica';
                    $cadastro = $arrayDestinatarios['cnpj'];
                }
                
                echo '<tr><form action="/" method="POST"><input type="hidden" name="operacao" value="alterarDestinatario" />
                <input type="hidden" name="id" value="'.$arrayDestinatarios['id'].'" />
                <input type="hidden" name="pessoa" value="'.$arrayDestinatarios['pessoa'].'" />';
                    
                if ($arrayDestinatarios['pessoa'] == 1){
                    echo '<input type="hidden" name="cadastro" value="'.$arrayDestinatarios['cpf'].'" />';                    
                }
                else if ($arrayDestinatarios['pessoa'] == 2){
                    echo '<input type="hidden" name="cadastro" value="'.$arrayDestinatarios['cnpj'].'" />';                    
                }
                echo '<td align="center"><input type="text" name="nome" value="'.stripslashes($arrayDestinatarios['nome']).'"  size="25"/></td>';                
                echo '<td align="center">'.$pessoa.'</td>';                              
                echo '<td align="center">'.$cadastro.'</td>';
                echo '<td align="center"><input type="text" name="endereco" value="'.stripslashes($arrayDestinatarios['endereco']).'" size="30"/></td>';
                echo '<td align="center"><input type="text" name="municipio" value="'.stripslashes($arrayDestinatarios['municipio']).'" size="30" /></td>';
                echo '<td align="center"><input type="submit" value="Alterar" /></td></form>';
                echo '</tr>';
            }
            echo '</table>';
        
        }
            if ($comum == 'cadNewDestinatario'){
                ?>
        <h2 align="center">Cadastro de destinatário (Nota Fiscal)</h2><p>&nbsp;</p>
        <form action="/" method="POST" name="form">
                <input type="hidden" name="operacao" value="cadDestinatario" />
                <input type="hidden" name="pagina" value="<?php echo $_GET['pagina'];?>" />
                <input type="hidden" name="idPropriedade" value="<?php echo $_GET['idPropriedade'];?>" />
                <input type="hidden" name="idProdutor" value="<?php echo $_GET['idProdutor'];?>" />
                <div align="center"><table width="500" cellspacing="10" cellpadding="0" align="center">
                    <tr>
                        <td width="64"><div align="right"><span class="style1">Nome:</span></div></td>
                        <td width="227"><label>
                            <input type="text" id="nome"  name="nome" size="30" maxlength="99" />
                            </label></td>
                        </td>
                    </tr>
                    <tr>
                        <td width="64"><div align="right"><span class="style1"></span></div></td>
                        <td width="227">
                            <label class="pessoa1">Pessoa física&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio"  name="pessoa" value="1" /></label>&nbsp;
                                <input type="text" class="cpf"  name="cpf" size="15" maxlength="99"/><br/><br/>
                   
                            <label class="pessoa2">Pessoa jurídica&nbsp;<input type="radio"  name="pessoa" value="2" /></label>&nbsp;
                                <input type="text" class="cnpj"  name="cnpj" size="15" maxlength="99"/>
                        </td>
                    </tr>

                    <tr>
                        <td width="64"><div align="right"><span class="style1">Endereço:</span></div></td>
                        <td width="227">
                            <input type="text"  name="endereco" size="30" maxlength="99"/>
                        </td>
                    </tr>
                    
                    <tr>
                        <td width="64"><div align="right"><span class="style1">Município:</span></div></td>
                        <td width="227"><input type="text"  name="municipio" size="30" maxlength="99"/>
                        </td>
                    </tr>
                    
                    <tr>
                        <td width="64"><div align="right"><span class="style1"></span></div></td>
                        <td width="227">
                            <input type="submit"  value="Cadastrar"/>
                        </td>
                    </tr>
                    
                    </table></div>
            </form>
        <?php
            }
        ?>
        
    </body>
</html>